
import 'package:flutter/material.dart';
import 'package:flutter_elf/_base/base_common.dart';
import 'package:flutter_elf/utils/my_route_observer.dart';
import 'package:flutter_elf/utils/print_utils.dart';

abstract class BaseWidget extends StatefulWidget{
  @override
  BaseWidgetState createState() {
    return getState();
  }
  BaseWidgetState getState();
}
final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
final RouteObserver<PageRoute> routeObserver2 = RouteObserver<PageRoute>();


abstract class BaseWidgetState<T extends BaseWidget> extends State<T>
    with WidgetsBindingObserver, BaseCommon ,AutomaticKeepAliveClientMixin,RouteAware {

  T get widget => super.widget;
  bool _isVisibleToUser = false; // 是否对用户可见，表明在onResume-onPause调用过程中。也用来表示当前页面是否在栈顶。
  //平台信息
  @override
  bool get wantKeepAlive => true;
  bool isFinished = false;
  BuildContext mContext;

  bool _systemDispatched = false;

  bool _didPush = false; // 表示是否调用了didPush方法。第一次进入会调用
  bool _didPopNext = false; // 表示是否调用了didPopNext方法。进入别的页面，再次返回时会调用。
  bool _didPop = false; // 表示是否调用了didPop方法。从Navigator中pop时会调用。

  int _didPopNextCount = 0;
  @override
  void initState() {
    super.initState();
    initBaseCommon(this, context);
    mContext = context;
    logs('当前界面是'   +  this.runtimeType.toString());
    _onMockCreate();
    _onMockResume();
    WidgetsBinding.instance.addObserver(this);
  }


  @override
  void deactivate() {
    super.deactivate();
    if (_didPop || _didPush || _didPopNextCount == 1) {
      _onMockPause();
    } else if (_didPopNext) {
      globalPrint('deactivate');
      _onMockResume();
    }
    _resetValues();
  }

  void _resetValues() {
    _didPop = false;
    _didPopNext = false;
    _didPush = false;
    _didPopNextCount--;
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    routeObserver.unsubscribe(this);

    _onMockDestroy();
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        globalPrint('didChangeAppLifecycleState');
        _onMockResume();
        _systemDispatched = false;
        break;
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        _systemDispatched = true;
        _onMockPause();
        break;
      default:
        break;
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context));
  }

  @override
  void didPush() {
    _didPush = true;
  }

  @override
  void didPopNext() {
    globalPrint(this.runtimeType.toString() + ' ====>didPopNext');
    _didPopNext = true;
    _didPopNextCount = 2;
  }

  @override
  void didPop() {
    _didPop = true;
  }

  void _onMockCreate() {
    onCreate();
  }

  @mustCallSuper
  void _onMockResume() {
    if (_systemDispatched && _isVisibleToUser || !_systemDispatched && !_isVisibleToUser) {
      _isVisibleToUser = true;
      onResume();
    }
  }

  @mustCallSuper
  void _onMockPause() {
    if (!_isVisibleToUser) {
      return;
    }
    if (!_systemDispatched) {
      _isVisibleToUser = false;
    }
    onPause();
  }

  void _onMockDestroy() {
    onDestroy();
  }

  @override
  void onDestroy(){

  }

  @override
  void onCreate() {

  }

  @override
  void onResume() {
    globalPrint(this.runtimeType.toString() + ' ====>onResume');
  }

  @override
  void onPause() {

  }

  var tagInStateWithLifecycle = '';

  @override
  // ignore: must_call_super
  Widget build(BuildContext context) {
    return getBaseView(context);
  }

  ScaffoldState _scaffoldState = new ScaffoldState();
  showSnackBar(String s){
    _scaffoldState.showSnackBar(SnackBar(content: Text(s)));
  }

  @override
  Widget getBottomBar(context) {
    return null;
  }
}